package com.qbw.app.module.vehicle.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;

import com.http.okhttp.HttpTask;
import com.lib.util.ScreenUtil;
import com.qbw.app.R;
import com.qbw.app.XApplication;
import com.qbw.app.databinding.VehicleActivityNotificationVehicleListBinding;
import com.qbw.app.model.Vehicle;
import com.qbw.app.module.common.activity.CommonActivity;
import com.qbw.app.module.common.util.HttpRequestUtil;
import com.qbw.app.module.common.util.UniversalToastUtil;
import com.qbw.app.module.vehicle.adapter.VehicleNotificationAdapter;
import com.qbw.app.request.impl.VehicleRequest;

/**
 * @author QBW
 * @date 2019-10-03
 */
public class VehicleNotificationVehicleListActivity extends CommonActivity<VehicleActivityNotificationVehicleListBinding> {

    private VehicleNotificationAdapter mAdapter;

    public static void start(Context context) {
        Intent intent = new Intent(context, VehicleNotificationVehicleListActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.vehicle_activity_notification_vehicle_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding.title.setListener(this);
        mBinding.rcv.setLayoutManager(new LinearLayoutManager(this));
        mBinding.rcv.addItemDecoration(new InnerDecoration());
        mAdapter = new VehicleNotificationAdapter(this);
        mBinding.rcv.setAdapter(mAdapter);
        showLoadingDialog("");
        mGeneralManager.getRequestManager().getVehicleRequest().getVehicleList(this).execute();
    }

    @Override
    public void onHttpSuccess(HttpTask httpTask, Object entity) {
        super.onHttpSuccess(httpTask, entity);
        String method = httpTask.getMethod();
        if (method.equals(VehicleRequest.METHOD_VEHICLE_LIST)) {
            Vehicle.Response response = (Vehicle.Response) entity;
            mAdapter.addChild(response.getData().getList());
            closeLoadingDialog();
        }
    }

    @Override
    public void onHttpFailed(HttpTask httpTask, int errorCode, String message) {
        super.onHttpFailed(httpTask, errorCode, message);
        message = HttpRequestUtil.getMessage(errorCode, message);
        String method = httpTask.getMethod();
        if (method.equals(VehicleRequest.METHOD_VEHICLE_LIST)) {
            UniversalToastUtil.showFailedIconToast(message);
            closeLoadingDialog();
            finish();
        }
    }

    private static class InnerDecoration extends RecyclerView.ItemDecoration {

        private Context mContext = XApplication.getInstance();
        private int mSpace1;
        private Paint mPaint;

        public InnerDecoration() {
            mSpace1 = ScreenUtil.dp2px(mContext, 15);
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setColor(Color.parseColor("#2B333C"));
        }

        @Override
        public void getItemOffsets(@NonNull Rect outRect,
                                   @NonNull View view,
                                   @NonNull RecyclerView parent,
                                   @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            int ap = parent.getChildAdapterPosition(view);
            if (ap == RecyclerView.NO_POSITION) {
                return;
            }
            if (ap == 0) {
                outRect.top = mSpace1;
            }
        }

        @Override
        public void onDrawOver(@NonNull Canvas c,
                               @NonNull RecyclerView parent,
                               @NonNull RecyclerView.State state) {
            super.onDrawOver(c, parent, state);
            int cc = parent.getChildCount();
            View view;
            for (int i = 0; i < cc; i++) {
                view = parent.getChildAt(i);
                c.drawRect(view.getLeft() + mSpace1,
                           view.getTop(),
                           view.getRight() - mSpace1,
                           view.getTop() + 1,
                           mPaint);
            }
            if (cc > 0) {
                view = parent.getChildAt(cc - 1);
                c.drawRect(view.getLeft() + mSpace1,
                           view.getBottom() - 1,
                           view.getRight() - mSpace1,
                           view.getBottom(),
                           mPaint);
            }
        }
    }
}
